<template>
  <div :class="`bwy-switch_${size}`" :style="{'--color': color}">
    <input :checked="modelValue" @change="upModelValue" class="yep" id="check-apple" type="checkbox">
    <label id="switch_label" for="check-apple"></label>
  </div>
</template>

<script setup>
import { toRefs } from "vue"

const props = defineProps({
    modelValue: {
        type: [Number,Boolean],
        default: false
    },
    size: {
        type: String,
        default: 'default' // small-小,default-默认,large-大号
    },
    color: {
        type: String,
        default: '#1677ff'
    }
})

const {modelValue} = toRefs(props)
const emit = defineEmits(['update:modelValue' , 'change'])

const upModelValue = () => { 
    emit("update:modelValue" , !modelValue.value)
    emit("change" , modelValue.value)
}
</script>

<style>
.bwy-switch_small {
    position: relative;
    width: 40px;
    height: 20px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
.bwy-switch_default{
  position: relative;
  width: 50px;
  height: 25px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
.bwy-switch_large{
  position: relative;
  width: 60px;
  height: 30px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

#switch_label {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50px;
  background: linear-gradient(to bottom, #b3b3b3, #e6e6e6);
  cursor: pointer;
  transition: all 0.3s ease;
}
#switch_label:after {
  content: '';
  position: absolute;
  top: 1px;
  left: 1px;
  width: calc((100% / 2) - 2px);
  height: calc((50% * 2) - 2px);
  border-radius: 50%;
  background-color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease;
}

#check-apple[type="checkbox"]:checked + label {
  background: var(--color);
  /* display: none; */
}

#check-apple[type="checkbox"]:checked + label:after {
  transform: translateX(calc(100% + 2px));
}

#switch_label:hover {
  background: linear-gradient(to bottom, #b3b3b3, #e6e6e6);
}

#switch_label:hover:after {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.yep {
  display: none;
}
</style>
